package cn.itqiqi.student_system_java.interceptor;

import cn.hutool.json.JSONUtil;
import cn.itqiqi.student_system_java.mapper.UserMapper;
import cn.itqiqi.student_system_java.pojo.User;
import cn.itqiqi.student_system_java.service.UserService;
import cn.itqiqi.student_system_java.service.impl.UserServiceImpl;
import cn.itqiqi.student_system_java.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.TreeMap;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description
 * @date 2022-06-20 15:18
 */
public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        String jsonStr;
        /**
         * token -use id info
         * admin student
         * admin -> redis
         * student -> redis
         *
         */
        if (token != null && !token.equals("")) {
            try {
                JWTUtils.verify(token);
                String username = JWTUtils.getTokenPayload(token).getClaim("username").asString();
                User user = userService.getUserByUserName(username);
                if (user == null) {
                    jsonStr = JSONUtil.toJsonStr(new TreeMap<Object, Object>() {
                        {
                            put("code", 1181);
                            put("msg", "拿假token糊弄鬼呢？？？");
                        }
                    });
                } else if (user.getAuthority() != 1) {
                    if (request.getMethod().equals("GET")) {
                        return true;
                    } else {
                        jsonStr = JSONUtil.toJsonStr(new TreeMap<Object, Object>() {
                            {
                                put("code", 1166);
                                put("msg", "对不起，你没有权限！");
                            }
                        });
                    }
                } else {
                    return true;
                }
            } catch (Exception e) {
                jsonStr = JSONUtil.toJsonStr(new TreeMap<Object, Object>() {
                    {
                        put("code", 1182);
                        put("msg", "会不会玩?别乱请求！！！");
                    }
                });
            }
        } else {
            jsonStr = JSONUtil.toJsonStr(new TreeMap<Object, Object>() {
                {
                    put("code", 1183);
                    put("msg", "你登陆了？？快去登录!");
                }
            });
        }
        if (jsonStr != null) {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(jsonStr);
        }
        return false;
    }
}
